import argparse
from eetop import EETOP

if __name__ == "__main__":

    parser = argparse.ArgumentParser(description="EETOP Automation Reply Agent")
    parser.add_argument("--username", type=str, required=True, help="Your EETOP username")
    parser.add_argument("--password", type=str, required=False, default='', help="Your EETOP password")
    parser.add_argument("--session", type=bool, required=False, default=True, help="First try use session, if not exist, then login")
    parser.add_argument("--forum_id", type=int, required=True, help="Forum ID to reply to")
    parser.add_argument("--page_num", type=int, required=True, help="Page number to reply to")
    args = parser.parse_args()

    if not args.session and (args.username=='' or args.password==''):
        parser.print_help()
        exit(1)

    eetop = EETOP(args.username, args.password)

    # 尝试 session
    if args.session:
        load_flag = eetop.load_session()
        # 若 session 加载失败, 且用户名和密码为空, 则退出
        if not load_flag and (args.username=='' or args.password==''):
            print("Session not found and no username/password provided. Exiting.")
            exit(1)
        # 加载 session 成功但登录失败
        if load_flag and not eetop.verify_login():
            # 尝试账号密码登陆
            if not eetop.login():
                print("Session loaded but login failed.")
                exit(1)
    # 账号密码登录
    else:
        if not eetop.login():
            print("Login failed.")
            exit(1)

    # 保存 session
    eetop.save_session()

    # 自动回复
    eetop.auto_quick_reply(args.forum_id, args.page_num, ['感谢分享', '正好学习一下', '感谢楼主分享'])
